package com.yz.crm.weixin.util;

import com.yz.common.property.PropertyReader;
import com.yz.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

import java.util.Map;
import java.util.Objects;

/**
 * Created by LiuJun on 2015/7/17.
 */
public class PropertyUtils {

    private static Logger log = Logger.getLogger(PropertyUtils.class);

    private static PropertyReader reader = PropertyReader.getInstance("common");

    public static int getValue(String key, int defaultValue) {
        int value = NumberUtils.toInt(Objects.toString(reader.getText(key), null), defaultValue);
        return value;
    }

    public static String getValue(String key, String defaultValue) {
        String value = Objects.toString(reader.getText(key), defaultValue);
        value = StringUtils.trim(value);
        return value;
    }


    public static int getValue(String key, int defaultValue, String[] param) {
        try {
            int value = NumberUtils.toInt(Objects.toString(reader.getMessage(key, param)), defaultValue);
            return value;
        } catch (Exception e) {
            log.info("file:common"  + ",key:" + key + ",defaultValue:" + defaultValue + ",param" + param + "error");
        }
        return getValue(key, defaultValue);
    }

    public static String getValue(String key, String defaultValue, String[] param) {
        try {
            String value = Objects.toString(reader.getMessage(key, param), defaultValue);
            value = StringUtils.trim(value);
            return value;
        } catch (Exception e) {
            log.info("file:common,key:" + key + ",defaultValue:" + defaultValue + ",param" + param + "error");
        }
        return getValue(key, defaultValue);

    }

    /**
     * 得到MSG
     */
    public static String getMsg(String key, Object... args) {
        return reader.getMessage(key, args);
    }

    /**
     * 是否包含key
     */
    public static boolean contains(String key) {
        return reader.containsKey(key);
    }

    public static String getCookieId() {
        return "cookid";
    }



    /**
     * 得到资源访问起始地址
     */
    public static String getUploadResourceRootUrl() {
        return getValue("uploadResUrl", "/");
    }

    /**
     * 得到本地服务访问URL
     */
    public static String getLocalServerUrl() {
        return getValue("LocalServer", "");
    }

    /**
     * 得到发布服务访问URL
     */
    public static String getPublishServerUrl() {
        return getValue("LocalServer", "");
    }

    /**
     * 得到邮件服务访问URL
     */
    public static String getOpenServerUrl() {
        return getValue("OpenServer", "");
    }
    
    /**
     * 得到WebSocket连接URL
     */
    public static String getWebSocketServerUrl() {
    	return getValue("WebSocketServer", "");
    }

    /**
     * 得到邮件服务访问URL
     */
    public static Map<String, String> getDefaultMailConfig() {
        return reader.getValues("edm.default");
    }
}
